# -*- coding: utf-8 -*-

from w.settings import W

from w.html.doctypes.preferable import Div
from w.html.base import ITag

import functools

from w.utils.general import isLikeList

class IBlock(ITag):
    tag = None
    
    cssFiles = []
    
    _document = None

    generator = W.BLOCKS.ID_GENERATOR_CLASS()

    def cssCode(self):
        return ''

    def __init__(self, document=None, *argv, **kwargv):
        #TODO:mmm?
        self.className = self.__class__.__name__
        self.blockID = self.generator.getID(self.className.lower())

        self._document = document

class Block(IBlock):
    _parent = None
    emptyContentReplacement = ''
    
    errorBlock = functools.partial(Div, hclass='w_blocks_Block_errorBlock')
    helpBlock = functools.partial(Div, hclass='w_blocks_Block_helpBlock')
    labelBlock = functools.partial(Div, hclass='w_blocks_Block_labelBlock')

    def __init__(self,  content=None, 
                        label=None, help=None, error=None, **kwargs):
        IBlock.__init__(self, **kwargs)
        self.help = help
        self.error = error
        self.label = label

        if content == None:
             content = self.emptyContentReplacement

        self += content if isLikeList(content) and not isinstance(content, ITag) else [content,]

    def make(self, content=[]):
        if not content:
            content = list(self)
        if self.label:
            content = [self.labelBlock(self.label)]+content
        if self.error:
            content.append(self.errorBlock(self.error))
        elif self.help:
            content.append(self.helpBlock(self.help))
        return content


    def __str__(self):
        return ''.join([str(s) for s in self.make()])
